package com.dcy.znoa.service.conference.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.conference.model.operGuest.SelectOperGuestModel;
import com.dcy.znoa.controller.publiz.model.messNotice.AddMessNoticeListAndMsgModel;
import com.dcy.znoa.dao.conference.OperGuestDao;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.domain.conference.OperGuest;
import com.dcy.znoa.domain.conference.OperGuestAppVo;
import com.dcy.znoa.domain.conference.OperGuestVo;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.service.conference.OperGuestService;
import com.dcy.znoa.service.publiz.MessNoticeService;

@Service
public class OperGuestServiceImpl implements OperGuestService {
	@Autowired
	OperGuestDao operGuestDao;
	
	@Autowired
    MessNoticeService messNoticeService;
	
	@Autowired
	CompanyDao companyDao;
	
	@Override
	public JsonCommon insert(OperGuest operGuest) throws InvocationTargetException, IllegalAccessException, BusinessException, ClientException {
		Integer result = operGuestDao.insert(operGuest);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        
        AddMessNoticeListAndMsgModel amnlModel = new AddMessNoticeListAndMsgModel();
        amnlModel.setMessTitle("来访通知");
        amnlModel.setMessage(operGuest.getGName()+"来访");
        amnlModel.setMessType(7);//访客
        amnlModel.setSendTime(operGuest.getCreateTime());
        amnlModel.setCreateOperId(operGuest.getCreateOperId());
        amnlModel.setCreateTime(operGuest.getCreateTime());
        amnlModel.setValidFlag(1);
        amnlModel.setIsSms(0);
       
        amnlModel.setOperIdsStr(operGuest.getOperId().toString());
        //ids
        amnlModel.setEntityId(operGuest.getGuestId());
        amnlModel.setEntityTbl("oper_guest");
        
        //给接收人的消息
        Long messId = messNoticeService.insertBatchAndMsg(amnlModel);
                
        JsonCommon jsonCommon = new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
        Company company = companyDao.selectById(operGuest.getCompId());
        if (null !=  company) {
        	JSONObject data = new JSONObject();
            data.put("compId", company.getCompId());
            data.put("compName", company.getCompName());
            data.put("qrCode", operGuest.getQrCode());
            jsonCommon.setData(data);
		}
        
        return jsonCommon;
	}

	@Override
	public JsonCommon updateById(OperGuest operGuest) {		
		OperGuest operGuestTemp = operGuestDao.selectById(operGuest.getGuestId());
		//判断是否能够取消
		Date currentTime = new Date();
		//状态没有变成已会见之前的任意一个状态都可以取消
		//不论是TO_AUDIT还是待会见
		//发起者和被访者都可取消

		if (null != operGuestTemp) {
			if (operGuestTemp.getValidFlag() == 1 && null != operGuest.getValidFlag() && operGuest.getValidFlag() == 0) {  //进行取消操作
				if ( !operGuestTemp.getGuestTime().after(currentTime) ) {
					return  new JsonCommon("404","访问时间已过");
				}
				
				if (operGuestTemp.getAuditStatus() == 2 ) {
					return  new JsonCommon("404","已经审批拒绝访问");
				}
				
				//被访者取消来访
				if (operGuest.getUpdateOperId().longValue() == operGuestTemp.getOperId().longValue()) {
					if (operGuestTemp.getAuditStatus() == 0 && operGuestTemp.getGuestTime().after(currentTime) ) {
						return  new JsonCommon("404","您可以审批拒绝此次访问");
					}
				}
				
			}
			
			Integer result = operGuestDao.updateById(operGuest);
	        if (result == null || result <= 0) {
	            return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE);
	        }
	        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
		
		
        return new JsonCommon("404", "未查找到此预约");
	}

	@Override
	public JsonCommon selectById(Long guestId) {
		OperGuest operGuest = operGuestDao.selectById(guestId);
		
        return new JsonCommon(operGuest,JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonForList selectList(SelectOperGuestModel model) {
        List<OperGuestVo> list = operGuestDao.selectOperGuestList(model);		
		        
        Integer total = operGuestDao.selectOperGuestCount(model);
		
		return new JsonForList(list, total);	
	}

	@Override
	public JsonForList selectOperGuestStatusList(SelectOperGuestModel model) {
		List<OperGuestAppVo> list = operGuestDao.selectOperGuestStatusList(model);	
		if (list.isEmpty()) {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		} else {
			return new JsonForList(list, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
	}

	@Override
	public boolean canAllowed(Long operId, Long guestId) {
		OperGuest operGuest = operGuestDao.selectById(guestId);
		if (operGuest != null && operGuest.getOperId().longValue() == operId.longValue()) {
			//被访者自己才能审批
			return true;
		}
		return false;
	}

	@Override
	public JsonCommon selectByQrCode(String qrCode) {
		List<OperGuestVo> list = operGuestDao.selectByQrCode(qrCode);
		if (list != null && !list.isEmpty()) {
			
			return new JsonCommon(list.get(0),JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
		return new JsonCommon("404", "未查找到此预约");
	}



}
